package com.imooc.alllearning.controller;

import com.imooc.alllearning.common.ResponseResult;
import com.imooc.alllearning.exception.BusinessException;
import com.imooc.alllearning.exception.ErrorCodeEnum;
import com.imooc.alllearning.service.ExcelExportService;
import com.imooc.alllearning.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * @Author: crowsjian
 * @Date: 2020/3/31 23:03
 */
@RestController
@RequestMapping("/api/files")
@Slf4j
public class FileController {
    @Resource(name="localFileServiceImpl")
    private FileService fileService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResponseResult<String> upload(@NotNull MultipartFile file){
        //文件上传
        try{
            fileService.upload(file.getInputStream(), file.getOriginalFilename());
        }catch (Exception e){
            log.error("文件上传失败！",e );
            throw new BusinessException(ErrorCodeEnum.UPLOAD_FILE_ERROR, e);
        }
        return ResponseResult.success(file.getOriginalFilename());
    }
}
